package com.xuecheng.content.controller;

import com.xuecheng.commons.model.dto.BindTeachplanMediaDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.dto.TeachplanDto;
import com.xuecheng.commons.model.dto.TeachplanWorkDto;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.TeachplanVo;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.content.service.TeachplanWorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 课程计划前端控制器
 */
@Slf4j
@RestController
@RequestMapping("/teachplan")
public class TeachplanController {

    @Autowired
    private TeachplanService  teachplanService;

    /**
     * 根据课程id，查询课程的章节目录
     * 地址：GET： /{courseId}/tree-nodes
     * 参数：课程id
     * 响应 ResponseResult<TeachplanVo>
     */
    @GetMapping("/{courseId}/tree-nodes")
    public ResponseResult<TeachplanVo> treeNodes(@PathVariable("courseId") Long courseId) {
        return teachplanService.treeNodes(courseId);
    }

    /**
     * 保存或者更新课程计划
     * 地址： POST
     * 参数：TeachplanDto
     * 响应：ResponseResult
     */
    @PostMapping
    public ResponseResult saveOrUpdatePlan(@RequestBody TeachplanDto dto) {
        return teachplanService.saveOrUpdatePlan(dto);
    }

    /**
     * 课程计划绑定媒资
     * Path：POST /media/association
     * 参数：BindTeachplanMediaDto
     * 响应：ResponseResult
     */
    @PostMapping("/media/association")
    public ResponseResult association(@RequestBody BindTeachplanMediaDto dto) {
        return teachplanService.association(dto);
    }

    /**
     * 根据id查询课程计划
     *   get /{teachplanId}"
     *   TeachplanVo
     */
    @GetMapping("/{teachplanId}")
    public ResponseResult<TeachplanVo> findById(@PathVariable("teachplanId") Long teachplanId) {
        Teachplan teachplan = teachplanService.getById(teachplanId);
        TeachplanVo vo = BeanHelper.copyProperties(teachplan, TeachplanVo.class);
        return ResponseResult.okResult(vo);
    }

    //教学计划-作业绑定
    /*
     * post /work/association
     * 请求参数  TeachplanWorkDto
     * */
    @PostMapping("/work/association")
    public ResponseResult association(@RequestBody TeachplanWorkDto dto) {
        return teachplanService.teachplanWorkAssociation(dto);
    }
    @Autowired
    private TeachplanWorkService teachplanWorkService;
    //作业解绑
    @DeleteMapping("/work/{teachplanWorkId}")
    public ResponseResult<Boolean> jieBang(@PathVariable("teachplanWorkId") Long teachplanWorkId, PageRequest request) {
        return teachplanWorkService.jiebang(teachplanWorkId, request);
    }
}
